Julia has been touted as the next best thing in computational programming, potentially replacing fan favourite, Python, as the go to language for data science and machine learning.

I have been looking to learn Julia for some time, and have finally gathered the courage to begin, following the MIT's 2021 Computational Thinking course, JuliaImages documentation and Tutorialspoint tutorial. The hope is to use Julia for quantitative economics, agent-based models and related interests (and some data science and machine learning of course!).

Some introductory concepts like installation, environments, packages and variables I have skipped to dive into the language. The Tutorialspoint tutorial gives quite good explanations for these.

Arrays are key to computational work, providing the building blocks for programming work in data science and machine learning. In this piece, I will use images to explore arrays. An array in Julia is a set of items "specified with square brackets" that can contain similar items or different items.

  1. Creating arrays...
arr = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3
arr1 = ["Leonard", tan, RGB(0, 1, 1, 1.245)]
Array[1:10]
1-element Vector{Array}:
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr2 = Array{Int64}(undef, 2, 2) #Create a 2x2 array with undefined values of type Integer64
Array[1:5,5:9]
2-element Vector{Array}:
 [1, 2, 3, 4, 5]
 [5, 6, 7, 8, 9]
collect(1:2:20)
10-element Vector{Int64}:
  1
  3
  5
  7
  9
 11
 13
 15
 17
 19
arr3 = collect(range(1, 15, 30))
30-element Vector{Float64}:
  1.0
  1.4827586206896552
  1.9655172413793103
  2.4482758620689653
  2.9310344827586206
  3.413793103448276
  3.896551724137931
  4.379310344827586
  4.862068965517241
  5.344827586206897
  ⋮
 11.137931034482758
 11.620689655172415
 12.10344827586207
 12.586206896551724
 13.068965517241379
 13.551724137931034
 14.03448275862069
 14.517241379310345
 15.0

Collect uses (start:step:stop) format whilst range uses (start, stop, length).

[1:10...] #using the ellipses
10-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

A matrix can be created by specifying the arrays, separating the rows with a semi-colon (;)

matrix = [[1 2];[3 4]]
2×2 Matrix{Int64}:
 1  2
 3  4

Compare with..

arr4 = [[1 2], [3 4]]
2-element Vector{Matrix{Int64}}:
 [1 2]
 [3 4]
tensor = Array{Float32}(undef, 3, 3, 3)
3×3×3 Array{Float32, 3}:
[:, :, 1] =
 0.0      0.0      4.0f-45
 0.0      3.0f-45  0.0
 1.0f-45  0.0      6.0f-45

[:, :, 2] =
 0.0      7.0f-45  0.0
 7.0f-45  0.0      1.0f-44
 0.0      1.0f-44  0.0

[:, :, 3] =
 1.3f-44  0.0      6.0f-45
 0.0      1.5f-44  0.0
 1.3f-44  0.0      1.8f-44
  1. Manipulating arrays..

These functions change the array in place, nota bene.

push!(arr, 10)
4-element Vector{Int64}:
  1
  2
  3
 10
pushfirst!(arr, 27)
5-element Vector{Int64}:
 27
  1
  2
  3
 10
splice!(arr, 3, 12)
2
pop!(arr)
  1. Arrays with images
using Images #importing a package (use add Package to install the package)
path_to_image = "..\\images\\Brain-Teaser2.png"
img = load(path_to_image)
Julia, in the notebook automatically shows the read image. Cool! Also Julia strings are double quotes ONLY, nota bene.
size(img)
(578, 1255)
img[100, 200]
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
RGB(0, 0, 1)
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
Another really cool feature from Julia. Can you do this in Python?
brain = img[100:470, 350:900]
save("..\\images\\brain.png", brain)
All errors:
===========================================
Could not open .\imagesrain.png for writing
===========================================
WriteBlob Failed `.\imagesrain.png' @ error/png.c/MagickPNGErrorHandler/1641
===========================================
SystemError: opening file ".\\images\brain.png": Invalid argument
===========================================
Errors encountered while save FileIO.File{FileIO.DataFormat{:PNG}, String}(".\\images\brain.png").

Fatal error:
0

The save function worked, but why it threw those errors, I am not sure yet. Errors in Julia I haven't yet grasped. Still yet to see a language with helpful error messages!

Kaleidoscope The last element of an array is "end" compared to -1 in python

[
    img img[:, end:-1:1]
    img[end:-1:1,:] img[end:-1:1, end:-1:1]
]
size(pex)
(750, 1042)
imgc = rand(RGB{Base.Float32}, 3, 3)
dump(imgc) #dump gives the internal representation of an object
Array{RGB{Float32}}((3, 3))
  1: RGB{Float32}
    r: Float32 0.11253506f0
    g: Float32 0.55760473f0
    b: Float32 0.27864915f0
  2: RGB{Float32}
    r: Float32 0.18111825f0
    g: Float32 0.28725207f0
    b: Float32 0.9795578f0
  3: RGB{Float32}
    r: Float32 0.28679127f0
    g: Float32 0.43194884f0
    b: Float32 0.5378687f0
  4: RGB{Float32}
    r: Float32 0.4753642f0
    g: Float32 0.035165608f0
    b: Float32 0.88715f0
  5: RGB{Float32}
    r: Float32 0.4875096f0
    g: Float32 0.20408314f0
    b: Float32 0.01918888f0
  6: RGB{Float32}
    r: Float32 0.18497908f0
    g: Float32 0.1815986f0
    b: Float32 0.276653f0
  7: RGB{Float32}
    r: Float32 0.67196274f0
    g: Float32 0.4420526f0
    b: Float32 0.50409055f0
  8: RGB{Float32}
    r: Float32 0.44515795f0
    g: Float32 0.70845145f0
    b: Float32 0.99803203f0
  9: RGB{Float32}
    r: Float32 0.4571224f0
    g: Float32 0.5548946f0
    b: Float32 0.38366348f0
imgc[2,3]
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
dump(imgc[2,3])
RGB{Float32}
  r: Float32 0.44515795f0
  g: Float32 0.70845145f0
  b: Float32 0.99803203f0
c = imgc[2,3]; (red(c), green(c), blue(c))
(0.44515795f0, 0.99803203f0, 0.70845145f0)
[RGB(x, 1, 1) for x in 0:0.1:1.0]
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
[
    RGB(x, y, 1) for x in 0:0.1:1.0, y in 0:0.1:1.0
]

Other cool things in Julia to do with images. To be explored more in a later post. Mosaicview makes it easy to juxtapose images, or stack them. Rot180 and reverse are self explaining.

pex = load("./images/pexels-photo-2422290.jpeg")
mosaicview(img, pex)
mosaicview(img, pex; nrow=1)
rot180(img)
reverse(pex)
  1. Synthetic images using random numbers
img_gen = randn(300, 250, 3)
300×250×3 Array{Float64, 3}:
[:, :, 1] =
 -0.600125   -1.56632     -0.312504   …  -0.103091  -0.271007   -1.13409
  0.244688    1.25623      0.259431      -0.636025  -0.208713    0.315366
 -1.22143    -0.59499      0.604384      -0.116677   1.16373    -0.0722718
 -0.622335   -1.08539     -0.724112       0.609908  -0.0189383  -0.128967
  1.35767     1.31528     -0.249722      -1.36404   -0.293069    0.556582
  0.534793    1.27606     -0.154081   …   1.12524    0.552972    1.02783
  0.319008    0.00880301   0.220965       1.11737    0.779357   -0.309602
 -0.362791   -1.31784     -0.253949       0.353058  -0.453932   -0.574562
 -0.120969   -0.962137     1.34985        2.07872   -0.94339    -0.0697277
  0.0511402  -1.99099      0.736905       1.61943    2.2774     -0.513229
  ⋮                                   ⋱                         
 -0.323872   -0.181434     1.12721       -1.13482    0.335018    0.868675
  0.603353   -0.606318     0.939072       0.048147   0.316767    0.275852
  0.977469   -1.24712     -1.44946        0.3013    -1.0562     -0.800215
  2.2684      1.51708      0.621769       1.23412    0.656476    0.201373
 -0.136838    0.634701    -0.215518   …  -0.201611  -2.17347     1.6066
 -1.24558     0.0652409    0.0804678     -0.854146  -0.578367    1.92466
  2.00107    -0.596724     1.53221        1.57971    0.84556    -0.47681
 -0.439637   -0.196832    -1.69774       -0.5361    -0.582335    1.0349
 -0.304142   -0.622998    -1.41491       -0.600071   2.76946     0.676878

[:, :, 2] =
 -1.07388    -0.605201  -0.213867   …   0.853067   -0.291855  -2.45098
  0.129474   -0.121532   1.2076        -0.0610356  -2.04145    0.00149836
  1.27809     0.779509   0.640596       1.35758     0.450349   2.08358
  0.0636016   1.07457    0.0605442      0.110871   -1.66489    1.3797
 -0.244607   -0.307671   0.357756      -0.436152    0.438603   0.0740943
  1.25962     0.742925   0.38487    …  -0.720242   -0.586007   0.440213
 -1.36131     1.45913   -1.26939       -0.3846     -0.570169  -0.0160573
  0.754807    1.03674   -0.443173       0.468278    0.892556  -0.586991
 -1.77824     0.839453  -1.48134       -1.22049     0.279893   1.58792
  1.0486      0.604839  -0.161788       0.292014   -1.75065    0.67918
  ⋮                                 ⋱                         
  1.94354    -1.4899    -0.300526      -2.11149    -0.536682   1.23141
 -1.80439    -0.384776  -1.01934       -0.31576    -0.743674  -0.735456
 -2.11735    -0.409913  -0.147772       0.604897   -0.416731  -0.3253
  0.117426   -0.591422  -1.71241       -1.42072     1.67098    2.64896
 -0.0323333   1.05344   -1.36912    …  -0.0445616  -1.00345    1.59313
 -0.265179    0.501541  -2.47764       -1.71942    -0.628153   0.0107511
  0.630664    0.191456   0.291961       0.770989    0.470247  -0.239118
 -0.0167329  -1.78455   -1.55999       -1.14523    -1.08318   -0.506993
 -2.39033    -0.799303  -0.51274        0.0174815  -0.31149    0.127548

[:, :, 3] =
  0.140216   -0.485375    0.267151   …  -0.385111   2.16931    -0.353331
 -0.561392   -0.841544    1.90666       -1.6451     0.416531   -0.564599
 -0.0901542   0.617174    1.20817       -0.698607  -0.0417566   0.0464137
  1.02936     0.509171   -1.35276       -1.0066     1.54414    -0.0431321
  0.697595    0.639903    0.150722       0.468857  -1.11395     1.33858
 -0.923934    0.232699    1.3056     …  -0.142713  -1.5119     -0.223377
 -0.989095   -0.255251    0.0348486     -0.721624  -2.00278     0.331685
 -0.0124481   1.52724     1.43884        0.267863   1.34697    -1.29293
  0.0802858   0.395102   -0.0679597      1.22957   -0.0325846   0.949232
 -0.324235    0.555573    1.09           0.254719   1.37643     1.80199
  ⋮                                  ⋱                         
  1.12088     0.120046    0.199124      -0.982841  -0.692224   -1.05452
  0.312383    0.0519733   0.258929       0.843973   0.714431   -1.20695
 -1.27403    -0.639111   -3.03254       -1.10927    0.723798    0.0525366
  0.861625   -0.698227   -1.35669        1.3572     0.399639   -0.0544047
  0.144619    0.807803   -0.733975   …  -1.10102   -0.401426    0.574467
 -0.203666   -0.848899    0.0437872     -0.637434   0.122017   -0.475935
 -0.843858    0.189737   -0.0541529     -1.96681    1.76856    -0.518815
  1.378       0.342705    1.2765         0.018891   0.833686    0.344957
 -0.105764   -0.368011    0.23163        0.917879   0.243322    0.616008
colorview(RGB, reshape(img_gen, 3, 300, 250))
colorview(RGB, reshape(img_gen, (3, 250, 300)))